package com.hmall.config;

import com.hmall.service.SearchService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ItemListener {

    @Autowired
    private SearchService searchService;

    //接收上架消息
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("up.queue"),
            exchange = @Exchange(name = "item.topic",type = ExchangeTypes.TOPIC),
            key = {"item.up"}
    ))
    public void listenItemUp(Long id){
        searchService.saveItemById(id);
        System.out.println("接收到请求");
    }

    //接收下架消息
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("down.queue"),
            exchange = @Exchange(name = "item.topic",type = ExchangeTypes.TOPIC),
            key = {"item.down"}
    ))
    public void listenItemDown(Long id){
        searchService.deleteItemById(id);
        System.out.println("接收到请求");
    }
}
